<?php
/**
 * @Author: king
 * @Last Modified by:   king
 * Date: 2018/6/7
 * Time: 11:06
 */
namespace App\Tools;

class Curl
{


    /**
     * 发起GET请求
     * @param string $url
     * @return mixed
     */
    public static function get($url='')
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
    /**
     * 发起HTTPS请求
     */
    public static function post($url,$data,$header,$type = 'json',$post=1)
    {
        $ch = curl_init();
        $res= curl_setopt ($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt ($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POST, $post);
        if($post) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
        $result = curl_exec ($ch);
        //连接失败
        if($result == FALSE)
        {
            if($type=='json')
            {
                $result = json(['code'=>10052,'msg'=>'Network error.']);
            }
            else
            {
                $result = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Response><code>0</code><msg>网络错误</msg></Response>";
            }
        }
        curl_close($ch);
        return $result;
    }
}